package com.sj.purchase.modules.cost.web;

import com.sj.purchase.modules.cost.domain.Profit;
import com.sj.purchase.modules.cost.domain.ProfitId;
import com.sj.purchase.modules.cost.repository.ProfitRepository;
import com.sj.purchase.modules.cost.utils.DoubleUtil;
import com.sj.purchase.modules.cost.views.VProfit;
import com.sj.purchase.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: songkaizong
 * 18-1-9 下午4:52
 */
@RestController
@RequestMapping("/profit")
public class ProfitController {
    @Autowired
    private ProfitRepository profitRepository;

    @GetMapping
    public ResponseEntity get(@DateTimeFormat(pattern = "yyyy-MM-dd") Date day) {
        Profit sj = profitRepository.findOne(new ProfitId(day, "三际数码官方旗舰店"));
        Profit wg = profitRepository.findOne(new ProfitId(day, "三际手机采购网"));

        Date yestDay = DateUtils.addDays(day, -1);
        Profit oldSj = profitRepository.findOne(new ProfitId(yestDay, "三际数码官方旗舰店"));
        Profit oldWg = profitRepository.findOne(new ProfitId(yestDay, "三际手机采购网"));

        double sjAvgProfitIncrease = division(sj.getProfit(), sj.getOrderNum()) - division(oldSj.getProfit(), oldSj.getOrderNum());//串码 利润增长
        double sjpjProfitIncrease = division(sj.getAvgPriceProfit(), sj.getOrderNum()) - division(oldSj.getAvgPriceProfit(), oldSj.getOrderNum());//平均价利润增长
        double wgAvgProfitIncrease = division(wg.getProfit(), wg.getOrderNum()) - division(oldWg.getProfit(), oldWg.getOrderNum());//串码 利润增长
        double wgpjProfitIncrease = division(wg.getAvgPriceProfit(), wg.getOrderNum()) - division(oldWg.getAvgPriceProfit(), oldWg.getOrderNum());//平均价利润增长
        VProfit sjProfit = new VProfit(sj.getId().getShopName(), sj.getOrderNum(), sj.getOrderNum() - oldSj.getOrderNum(),
                DoubleUtil.format(sj.getProfit()),
                DoubleUtil.format(division(sj.getProfit(), sj.getOrderNum())),
                DoubleUtil.format(sjAvgProfitIncrease), sj.getImeiNum(), day);
        sjProfit.setAvgPriceProfit(DoubleUtil.format(sj.getAvgPriceProfit()));
        sjProfit.setAvgPriceAvgProfit(DoubleUtil.format(division(sj.getAvgPriceProfit(), sj.getOrderNum())));
        sjProfit.setAvgPriceProfitIncrease(DoubleUtil.format(sjpjProfitIncrease));
        VProfit wgProfit = new VProfit(wg.getId().getShopName(), wg.getOrderNum(), wg.getOrderNum() - oldWg.getOrderNum(),
                DoubleUtil.format(wg.getProfit()),
                DoubleUtil.format(division(wg.getProfit(), wg.getOrderNum())),
                DoubleUtil.format(wgAvgProfitIncrease), wg.getImeiNum(), day);
        wgProfit.setAvgPriceProfit(DoubleUtil.format(wg.getAvgPriceProfit()));
        wgProfit.setAvgPriceAvgProfit(DoubleUtil.format(division(wg.getAvgPriceProfit(), wg.getOrderNum())));
        wgProfit.setAvgPriceProfitIncrease(DoubleUtil.format(wgpjProfitIncrease));
        List<VProfit> list = new ArrayList<>(2);
        list.add(sjProfit);
        list.add(wgProfit);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/list")
    public ResponseEntity list(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDay,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDay) {
        List<Profit> list = profitRepository.findByIdCreateDateBetween(startDay, endDay);
        List<Profit> result = list.stream().filter(profit -> profit.getId().getShopName().equals("三际手机采购网") ||
                profit.getId().getShopName().equals("三际数码官方旗舰店")).collect(Collectors.toList());
        return ResponseEntity.ok(result);
    }

    /**
     * 这里存在放假时利润与订单均为0的情况，利润为0则订单数基本也为0,直接短路。
     *
     * @param a
     * @param b
     * @return
     */
    private double division(double a, double b) {
        if (a == 0) return 0;
        else return a / b;
    }
}
